﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WindowsFormsApplication1
{
    public class WriteLog
    {
        private string fileName = "";

        /// <summary>

        /// 日志目录路径

        /// </summary>

        private string path = @".\Logs\";

        private System.IO.TextWriter output;

        public void WriteLogInfo(string message)
        {

            fileName = System.DateTime.Now.ToString("yyyy-MM-dd") + "log.txt";

            string absPath = path + fileName;

            string dt = System.Environment.NewLine + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            string outPar = System.Environment.NewLine + message;

            bool isFileExists = System.IO.File.Exists(absPath);

            if (isFileExists)
            {

                try
                {

                    output = System.IO.File.AppendText(absPath);

                    output.WriteLine(dt + outPar);

                    output.Close();

                }

                catch (Exception ex)
                {

                    throw new Exception(ex.Message);

                }

            }

            else
            {

                if (!Directory.Exists(path))
                {

                    Directory.CreateDirectory(path);

                }

                try
                {

                    output = System.IO.File.AppendText(absPath);

                    output.WriteLine(dt + outPar);

                    output.Close();

                }

                catch (Exception ex)
                {

                    throw new Exception(ex.Message);

                }

            }

        }

    }
}
